package com.jas.common;

import java.util.Calendar;

/**
 * This class is used for an <code>Event</code> object that has a starting and
 * ending date/time.
 * 
 */
public class TimedEvent extends Event {
	
	@SuppressWarnings("unused")
	private final String CLASS_NAME = this.getClass().getSimpleName(); // Used by the gson deserializer
	
	protected Calendar eventStartDateTime;
	protected Calendar eventEndDateTime;

	//@author a0118677r
	/**
	 * This constructor for the <code>TimedEvent</code> class sets the event's
	 * starting and ending date/time.
	 * 
	 * @param title
	 *            The title of this event.
	 * @param eventStartDateTime
	 *            A <code>Calendar</code> object of this event's starting
	 *            date/time.
	 * @param eventEndDateTime
	 *            A <code>Calendar</code> object of this event's ending
	 *            date/time.
	 * @throws EventDateException 
	 */
	public TimedEvent(String eventDescription, Calendar eventStartDateTime,
			Calendar eventEndDateTime) throws EventDateException {
		super();
		this.setEventDescription(eventDescription);
		this.setEventStartDateTime(eventStartDateTime);
		this.setEventEndDateTime(eventEndDateTime);
	}
	
	//@author a0118677r
	public TimedEvent(String eventId, String eventDescription, Calendar eventStartDateTime,
			Calendar eventEndDateTime) throws EventDateException {
		super(eventId);
		this.setEventDescription(eventDescription);
		this.setEventStartDateTime(eventStartDateTime);
		this.setEventEndDateTime(eventEndDateTime);
	}
	
	//@author a0118677r
	@Override
	public Event copy(){
		Event copy = null;
		try {
			copy = new TimedEvent(this.getEventId(), this.getEventDescription(), this.getEventStartDateTime(), this.getEventEndDateTime());
			copy.setCompleted(this.isCompleted());
		} catch (EventDateException e) {
			assert(false);
		}
		return copy;
	}

	// @author A0098803W
	/**
	 * This method returns a <code>Calendar</code> object of an event's starting
	 * date/time.
	 * 
	 * @return Returns a <code>Calendar</code> object of this event's starting
	 *         date/time.
	 */
	public Calendar getEventStartDateTime() {
		return eventStartDateTime;
	}

	//@author a0118677r
	/**
	 * This method sets a <code>Calendar</code> object to an event's starting
	 * date/time.
	 * 
	 * @param eventStartDateTime
	 *            The <code>Calendar</code> object of this event's starting
	 *            date/time.
	 * @throws EventDateException 
	 */
	public void setEventStartDateTime(Calendar eventStartDateTime) throws EventDateException {
		if(this.eventEndDateTime == null){
			this.eventStartDateTime = eventStartDateTime;
		}else if(this.eventEndDateTime.after(eventStartDateTime)){
			this.eventStartDateTime = eventStartDateTime;
		}else{
			throw new EventDateException(Language.getString("ERROR_END_BEFORE_START"));
		}
	}

	// @author A0098803W
	/**
	 * This method returns a <code>Calendar</code> object of an event's ending
	 * date/time.
	 * 
	 * @return Returns a <code>Calendar</code> object of this event's ending
	 *         date/time.
	 */
	public Calendar getEventEndDateTime() {
		return eventEndDateTime;
	}

	//@author a0118677r
	/**
	 * This method sets a <code>Calendar</code> object to an event's ending
	 * date/time.
	 * 
	 * @param eventEndDateTime
	 *            The <code>Calendar</code> object of this event's ending
	 *            date/time.
	 * @throws EventDateException 
	 */
	public void setEventEndDateTime(Calendar eventEndDateTime) throws EventDateException {
		if(this.eventStartDateTime.before(eventEndDateTime)){ 
			this.eventEndDateTime = eventEndDateTime;
		}else{
			throw new EventDateException(Language.getString("ERROR_END_BEFORE_START"));
		}
	}
	
	//@author a0118677r
	@SuppressWarnings("rawtypes")
	@Override
	public Class getSubClass() {
		return this.getClass();
	}

	//@author a0118677r
	@Override
	public String toString() {
		String startDateTime = Utilities.calendarToString(eventStartDateTime,Language.getString("DATE_FORMAT"));
		String endDateTime = Utilities.calendarToString(eventEndDateTime,Language.getString("DATE_FORMAT"));
		String completionStatus="";
		if(isCompleted()){
			completionStatus=Language.getString("KEYWORD_YES");
		}
		else{
			completionStatus=Language.getString("KEYWORD_NO");
		}
		return String.format(Language.getString("DISPLAY_EVENT"), getEventDescription(), startDateTime, endDateTime, completionStatus, getEventId());
	}
}
